package gwt.immunology.server.service;

import gwt.immunology.client.service.MedicineCardService;
import gwt.immunology.server.converter.MedicineCardDtoConverter;
import gwt.immunology.shared.GenericDto;
import gwt.immunology.shared.ResultSetDto;

import java.util.List;

import models.MedicineCard;
import play.Logger;
import play.modules.gwt2.GWT2Service;
import play.modules.gwt2.GWT2ServicePath;

@GWT2ServicePath("/immunology/medicineCard")
public class MedicineCardServiceImpl extends GWT2Service implements MedicineCardService {

    @Override
    public GenericDto registerNewMedicineCard(GenericDto medicineCardDto) {
	GenericDto result = null;
	if (medicineCardDto != null && !medicineCardDto.isEmpty()) {
	    MedicineCard medicineCard = MedicineCardDtoConverter.convertGenericDtoToMedicineCard(medicineCardDto);
	    Logger.info("registering new medicine card");
	    medicineCard = MedicineCard.em().merge(medicineCard);
	    result = MedicineCardDtoConverter.convertMedicineCardToGenericDto(medicineCard);
	} else {
	    Logger.warn("can't register medicine card with null value");
	}
	return result;
    }

    @Override
    public void deleteMedicineCardById(Long medicineCardId) {
	if (medicineCardId != null) {
	    MedicineCard medicineCard = MedicineCard.em().find(MedicineCard.class, medicineCardId);
	    if (medicineCard != null) {
		Logger.info("deleting medicine card by id = " + medicineCardId);
		MedicineCard.em().remove(medicineCard);
	    } else {
		Logger.warn("can't delete medicine card with null value");
	    }
	} else {
	    Logger.warn("can't delete medicine card by id = " + medicineCardId);
	}
    }

    @Override
    public GenericDto getMedicineCardById(Long medicineCardId) {
	GenericDto result = null;
	if (medicineCardId != null) {
	    Logger.info("retrieving medicine card by id = " + medicineCardId);
	    MedicineCard medicineCard = MedicineCard.em().find(MedicineCard.class, medicineCardId);
	    result = MedicineCardDtoConverter.convertMedicineCardToGenericDto(medicineCard);
	} else {
	    Logger.warn("can't retrieve medicine card by id = " + medicineCardId);
	}
	return result;
    }

    @Override
    public GenericDto editMedicineCard(GenericDto medicineCardDto) {
	GenericDto result = null;
	if (medicineCardDto != null && !medicineCardDto.isEmpty()) {
	    MedicineCard medicineCard = MedicineCardDtoConverter.convertGenericDtoToMedicineCard(medicineCardDto);
	    Logger.info("editing medicine card with id = " + medicineCard.medicineCardId);
	    medicineCard = MedicineCard.em().merge(medicineCard);
	    result = MedicineCardDtoConverter.convertMedicineCardToGenericDto(medicineCard);
	} else {
	    Logger.warn("can't edit medicine card with null value");
	}
	return result;
    }

    @Override
    public ResultSetDto getAllMedicineCards() {
	ResultSetDto result = null;
	Logger.info("retrieving all medicine cards");
	List<MedicineCard> medicineCards = MedicineCard.all().fetch();
	result = MedicineCardDtoConverter.convertMedicineCardsToResultSetDto(medicineCards);
	return result;
    }

}
